<template>
	<div class="system-user-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入用户名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" plain class="ml10" @click="onOpenAddUser">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增用户
				</el-button>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column type="index" width="50" />
				<el-table-column prop="username" label="账户名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="nickname" label="用户昵称" show-overflow-tooltip></el-table-column>
				<el-table-column label="关联角色" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.roles.map((v) => v.name).join(',') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="用户状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="describe" label="用户描述" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="created_at" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onOpenEditUser(scope.row)">修改</el-button>
						<el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="ruleForm.page"
				background
				v-model:page-size="ruleForm.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<AddUer :roleList="roleList" @refresh="getUserLists" ref="addUserRef" />
		<EditUser :roleList="roleList" @refresh="getUserLists" ref="editUserRef" />
	</div>
</template>

<script setup name="systemUser">
import { ElMessageBox, ElMessage } from 'element-plus';
import AddUer from '@/views/system/user/component/addUser.vue';
import EditUser from '@/views/system/user/component/editUser.vue';

import { getRoleList } from '@/api/system/role';
import { getUserList, delUser } from '@/api/system/user';

const addUserRef = ref();
const editUserRef = ref();

const tableData = reactive({
	data: [],
	total: 0,
});
const ruleForm = reactive({
	page: 1,
	limit: 10,
});
// 获取用户列表
const getUserLists = async () => {
	let res = await getUserList(ruleForm);
	if (res.code === 0) {
		tableData.data = res.data.data;
		tableData.total = res.data.total;
	} else {
		ElMessage.error(res.message);
	}
};
getUserLists();

// 分页改变
const onHandleSizeChange = (val) => {
	getUserLists();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	getUserLists();
};

// 获取角色列表
const roleList = ref([]);
const getRoleLists = async () => {
	let res = await getRoleList();
	if (res.code === 0) {
		roleList.value = res.data.data;
	} else {
		ElMessage.error(res.message);
	}
};
getRoleLists();

// 打开新增用户弹窗
const onOpenAddUser = () => {
	addUserRef.value.openDialog();
};
// 打开修改用户弹窗
const onOpenEditUser = (row) => {
	editUserRef.value.openDialog(row);
};
// 删除用户
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.username}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res = await delUser({ id: row.id });
			if (res.code === 0) {
				ElMessage.success('删除成功');
				getUserLists();
			} else {
				ElMessage.error('删除失败');
			}
		})
		.catch(() => {});
};
</script>
